const router = require('express').Router();
const db = require('../MySQLdb/dbQuery.js');
// 查询代办
router.get('/search', (req, res) => {
  let id = req.auth.id;
  let { key, datetime } = req.query;
  let sql = `SELECT state,group_concat(id) as id,group_concat(name) as name,group_concat(time) as time,group_concat(ingstate) as ingstate FROM Items where u_id=? and starttime<="${datetime}" and (endtime>="${datetime}" or endtime IS NULL) group by state;`;
  db.query(sql, [id], (status, data) => {
    if (!status) {
      return res.send(data);
    } else {
      let item = data.data;
      let yfinishlist = { state: 1, items: [] };
      let nfinishlist = { state: 0, items: [] };
      for (let j = 0; j < item.length; j++) {
        let itemid = item[j].id.split(',');
        let itemname = item[j].name.split(',');
        let itemtime = item[j].time.split(',');
        let itemingstate = item[j].ingstate.split(',');
        if (item[j].state) {
          // 已完成事件
          for (let i = 0; i < itemid.length; i++) {
            yfinishlist.items.push({
              id: itemid[i],
              name: itemname[i],
              time: itemtime[i],
              ingstate: itemingstate[i],
            });
          }
        } else {
          // 未完成事件
          for (let i = 0; i < itemid.length; i++) {
            nfinishlist.items.push({
              id: itemid[i],
              name: itemname[i],
              time: itemtime[i],
              ingstate: itemingstate[i],
            });
          }
        }
      }
      data.data = { y: yfinishlist, n: nfinishlist };
      return res.send(data);
    }
  });
});
// 添加代办
router.get('/add', (req, res) => {
  let id = req.auth.id;
  let { name, datetime } = req.query;
  let sql = `insert into Items(u_id,name,starttime)values(?,?,?);`;
  db.query(sql, [id, name, datetime], (status, data) => {
    if (!status) {
      return res.send(data);
    } else {
      data.data = '添加成功';
      return res.send(data);
    }
  });
});
// 修改代办
router.get('/update/:id', (req, res) => {
  let id = req.params.id;
  let item = req.query.item;
  let sql = 'update Items set name=? where id=?;';
  db.query(sql, [item, id], (static, data) => {
    if (!static) {
      return res.send(data);
    } else {
      data.data = '修改成功';
      return res.send(data);
    }
  });
});
// 删除代办
router.get('/delete/:id', (req, res) => {
  let id = req.params.id;
  let sql = 'delete from Items where id=?;';
  db.query(sql, [id], (static, data) => {
    if (!static) {
      return res.send(data);
    } else {
      data.data = '删除成功';
      return res.send(data);
    }
  });
});

// 切换代办状态
router.post('/switchstate', (req, res) => {
  let { id, state, datetime } = req.body;
  let uid = req.auth.id;
  let sql;
  if (state == 1) {
    sql = `update Items set state=?,endtime='${datetime}' where id=? and u_id=?;`;
  } else {
    sql = `update Items set state=? where id=? and u_id=?;`;
  }
  db.query(sql, [state, id, uid], (status, data) => {
    if (!status) {
      return res.send(data);
    } else {
      data.msg = '修改成功';
      return res.send(data);
    }
  });
});

// 更改代办完成时间
router.post('/settime', (req, res) => {
  let uid = req.auth.id;
  let { id, time } = req.body;
  let sql = 'update Items set time=? where u_id=? and id=?;';
  db.query(sql, [time, uid, id], (state, data) => {
    return res.send(data);
  });
});

// 开始执行
router.post('/action', (req, res) => {
  let uid = req.auth.id;
  let id = req.body.id;
  let sql = 'update Items set ingstate=1 where id=? and u_id=?';
  db.query(sql, [id, uid], (state, data) => {
    return res.send(data);
  });
});
// 停止执行
router.post('/stop', (req, res) => {
  let uid = req.auth.id;
  let { id, time } = req.body;
  let sql = 'update Items set ingstate=0,time=? where u_id=? and id=?;';
  db.query(sql, [time, uid, id], (state, data) => {
    return res.send(data);
  });
});
// 获取每日完成比值
router.post('/gethot', (req, res) => {
  let uid = req.auth.id;
  let sql =
    'select endtime as allstart,count(id) as num from Items where u_id=? group by endtime,state having state=1;';
  db.query(sql, [uid], (state, data) => {
    return res.send(data);
  });
});
module.exports = router;
